#include "BondDetailTitleEdit.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <qbbond/BondSearch.h>
#include <widgets/KeySearchWidget.h>
#include <widgets/SImageButton.h>

#include <QHBoxLayout>
#include <QStylePainter>

#include "BondDetailData.h"

BondDetailTitleEdit::BondDetailTitleEdit(QWidget* p) : QWidget(p) {
  m_searchWidget = new KeySearchWidget(this, "SearchBackNormal", "SearchHover",
                                       "BorderNormal");

  m_bondInterSearchE = new BondInterSearchEngine();
  m_searchWidget->setSearchEngine(m_bondInterSearchE);
  m_searchWidget->hide();
  m_searchWidget->setResultDlgMinWidth(380);
  m_searchWidget->setResultDlgMaxHeight(ITEM_DEFAULT_HEIGHT * 8);
  m_searchWidget->setColumnsWidth(4, 120, 150, 50, 50);

  QHBoxLayout* l = new QHBoxLayout(this);
  l->setContentsMargins(0, 4, 2, 4);
  l->setSpacing(4);

  m_favBtn = new SImageButton(this, "widgets_grid_star_normal");
  l->addWidget(m_favBtn);
  l->addWidget(m_searchWidget);
  l->addStretch();

  connect(m_searchWidget, &KeySearchWidget::searchFocusChanged, this,
          &BondDetailTitleEdit::onSearchFocusChanged);
}

void BondDetailTitleEdit::setDetailData(QSharedPointer<DetailData> d) {
  m_data = d;
  connect(d.get(), &DetailData::bondChanged, this,
          &BondDetailTitleEdit::loadBondInterSEInfo);
}

void BondDetailTitleEdit::loadBondInterSEInfo() {
  m_bondInterSearchE->setBondData(m_data);
}

void BondDetailTitleEdit::onSearchFocusChanged(bool v) {
  if (!v) {
    m_status = WidgetStatus::kNormal;
    m_searchWidget->hide();
    update();
  }
}

void BondDetailTitleEdit::mousePressEvent(QMouseEvent* event) {
  if (!m_searchWidget->isVisible()) {
    m_status = WidgetStatus::kPressed;
    m_searchWidget->setSearchText(m_data->shortName() + "   " + m_data->code());
    m_searchWidget->show();
    m_searchWidget->setFocus(Qt::FocusReason::MouseFocusReason);
    update();
  }
}

void BondDetailTitleEdit::enterEvent(QEvent* event) {
  if (!m_searchWidget->isVisible()) {
    this->setCursor(Qt::ArrowCursor);
    m_status = WidgetStatus::kHover;
    update();
  }
}

void BondDetailTitleEdit::leaveEvent(QEvent* event) {
  if (!m_searchWidget->isVisible()) {
    m_status = WidgetStatus::kNormal;
    update();
  }
}

void BondDetailTitleEdit::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);
  painter.setPen(Qt::transparent);
  QColor bkColor =
      StyleMgr::instance().color(m_status == kHover ? "Hover" : "BasicPlate");
  painter.setBrush(bkColor);

  QRect r = rect();
  int spacing = StyleMgr::instance().size("CtrlSpacing");
  r.adjust(0, spacing, 0, -spacing);
  painter.drawRect(r);

  if (m_status == kPressed) {
    return;
  }

  if (m_data == nullptr) {
    return;
  }

  QFont f = font();
  f.setPixelSize(18);
  f.setBold(true);
  painter.setFont(f);
  QFontMetrics fm(f, this);
  int w = fm.horizontalAdvance(m_data->shortName());

  r.setLeft(m_favBtn->geometry().right() + 4);
  painter.setPen(StyleMgr::instance().color("TextNormal"));
  painter.drawText(r, m_data->shortName(),
                   QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

  r.setLeft(r.left() + w + 20);
  painter.drawText(r, m_data->code(),
                   QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

  SImage* img = ImgMgr::instance().image("widgets_edit");
  if (img) {
    w = fm.horizontalAdvance(m_data->code());
    r.setLeft(r.left() + w + 2);
    r.setWidth(img->width());
    r.setTop(r.top() + (r.height() - img->height()) / 2);
    r.setHeight(img->height());
    painter.drawImage(r, *img);
  }
  // TODO:极端情况处理
}

void BondDetailTitleEdit::mouseReleaseEvent(QMouseEvent* event) {}

void BondDetailTitleEdit::resizeEvent(QResizeEvent* event) {
  m_searchWidget->setFixedWidth(rect().width() - m_favBtn->width() - 4);
}
